using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Awaiter;
using YoshiBase.Classes;
using jQueryApi;
using Nsb.Classes;
using Theme;

namespace Yoshi.Clusters
{
    [IgnoreNamespace]
    public class EmailInbox : EmailBase
    {
        private jQueryObject _inboxList;
        private jQueryObject _emptyNote;

        public EmailInbox(EmailCluster cluster)
            : base(cluster)
        {
        }

        public override EmailBase Build(bool active)
        {
            Initialize(active);
            Element.AddClass("Section");

            jQuery.FromHtml("<div class='Header'/>")
                .AppendTo(Element)
                .Html(EmailTheme.InboxTitle)
                ;

            _emptyNote = jQuery.FromHtml("<div class='InboxEmpty Hidden'/>")
                .AppendTo(Element)
                .Html(EmailTheme.NoConv)
                ;

            _inboxList = jQuery.FromHtml("<ul class='InboxList'/>")
                .AppendTo(Element)
                ;

            return this;
        }

        public override EmailBase Fill(object item)
        {
            throw Exceptions.NotImplementedException();
        }

        public void RefreshAw(Await awp)
        {
            new Await()
                .AddAw(Data.GetMailListAw)
                .HandleDl(Exceptions.BlobLoadError, Await.IgnoreAw)
                .AddDx(delegate(Await aw) { UpdateList((List<EmailListItem>)aw[PkgBase.ResultRx]); })
                .Commit(awp);
        }

        public void UpdateList(List<EmailListItem> list)
        {
            _inboxList.Empty();

            if (list == null)
            {
                _emptyNote.RemoveClass("Hidden");
                return;
            }

            _emptyNote.AddClass("Hidden");

            list = jQuery.ExtendObject(new List<EmailListItem>(), list);

            list.Sort(Sorter);

            foreach (EmailListItem item in list)
            {
                CreateItem(item).AppendTo(_inboxList);
            }
        }

        public static int Sorter(EmailListItem a, EmailListItem b)
        {
            if (a.unread != b.unread)
                return a.unread ? -1 : 1;
            return b.modifyTime - a.modifyTime;
        }

        private jQueryObject CreateItem(EmailListItem item)
        {
            jQueryObject line = jQuery.FromHtml("<li/>");

            jQuery.FromHtml("<span class='Alias'/>")
                .AppendTo(line)
                .Html(item.fAlias)
                .Attribute("title", item.fAlias)
                ;

            string msg = item.peek.Length > 54 ? item.peek.Substr(0, 50) + " ..." : item.peek;

            jQuery.FromHtml("<span class='Message'/>")
                .AppendTo(line)
                .Html(item.lAlias + " says - " + msg)
                //.Attribute("title", msg)
                ;

            if (item.unread)
                line.AddClass("Unread");

            line.Click(
                delegate
                {
                    jQuery.Select("li", Element).RemoveClass("Selected");
                    line.AddClass("Selected");

                    if (EmailProfile != null && EmailBubbles != null)
                    {
                        Await awx = new Await();
                        awx
                            .AddFn(EmailProfile.SetOther, item.fromId)
                            .AddFn(EmailProfile.FillFromIdAsync, item.fromId)
                            .AddAw(ScrollToAw, EmailProfile.Element)
                            .AddFn(EmailBubbles.ShowBubbles, item.fromId)
                            ;
                        if (item.unread && item.lastId != Data.My.id)
                        {
                            item.unread = false;
                            awx.AddAw(Data.UpdateStorageListsAw, Data.My.id, item.fromId, item);
                        }
                        awx.AddAw(RefreshAw);
                        awx.Commit();
                    }
                });

            return line;
        }
    }
}
